home *** CD-ROM | disk | FTP | other *** search
- Path: newsfeed.internetmci.com!panix!not-for-mail
- From: acinader@panix.com (Arthur Cinader Jr)
- Newsgroups: comp.lang.c++,gnu.help.g++
- Subject: Out of Memory error?
- Date: 17 Mar 1996 20:06:17 -0500
- Organization: Panix
- Message-ID: <4iicu9$kgm@panix.com>
- NNTP-Posting-Host: panix.com
-
- I must be making progress in my effort to learn to program,
- because this is a new error for me!
-
- My program uses a class Args which has two dynamically
- allocated arrays of strings one for labels and the other for values.
-
- An Args object is created by a static function in the Triangle
- class and passed to main. The values are loaded into the
- Args object in main and the object is then passed back to a
- "make instance of" static function in Triangle. However, when
- I try to load the third value, I get:
-
- assertion "val[valcnt] != 0" failed: file "args.C", line 49
- zsh: abort (core dumped) ./a.out
-
- A new one on me. I have run through the program, I know that
- it is crude, but I think that it should be working. The
- failed assert indicates to me that I have run out of
- memory...how can this be. What do I do now!?! Below is the
- code...any pointers in how to think about this type of issue/problem
- will be appreciated!
-
- Arthur
-
- ************
- // shape.h
- // declaration of abstract base class shape
- #ifndef SHAPE_H
- #define SHAPE_H
-
- class Shape {
- public:
- virtual void draw() const = 0; // pure virtual
- };
-
- #endif
-
- // point.h
- // Declaration for class Point which extends Shape
-
- #ifndef POINT_H
- #define POINT_H
-
- #include "shape.h"
-
- class Point : public Shape {
- public:
- Point(float = 0.0, float = 0.0);
- float getx() const;
- float gety() const;
- void setfill(char);
- virtual void draw() const;
-
- private:
- float x;
- float y;
- char fill;
- };
-
- #endif
-
- // point.C
- // definitions for abstract base class
-
- #include <iostream.h> // included for draw function to be
- // replaced with
- curses routine
- #include "point.h"
-
- // Constructor
- Point::Point(float a, float b)
- {
- x = a;
- y = b;
- setfill('*');
- }
-
- // Get x
- float Point::getx() const { return x; }
-
- // Get y
- float Point::gety() const { return y; }
-
- // Set fill
- void Point::setfill(char c) { fill = c; }
-
- // Draw
- void Point::draw() const
- {
- cout << "[" << x << ", " << y << "]" << endl;
- }
-
- // triangle.h
- // declaration of class Triangle extends shape
-
- #ifndef TRIANGLE_H
- #define TRIANGLE_H
-
- #include "point.h"
- #include "args.h"
-
- class Triangle : public Point {
- public:
- Triangle(float = 0.0, float = 0.0, float = 0.0);
- float gets() const;
- virtual void draw() const;
-
- static Args &getargs();
- static Triangle *makeinstance(Args &);
- private:
- float side;
- };
-
- #endif
-
- // triangle.C
- // Class definition
-
- #include <iostream.h>
- #include <string.h>
- #include <assert.h>
- #include <stdlib.h>
- #include "tri.h"
- #include "args.h"
-
- // Static Functions
-
- // Get Args
- Args &Triangle::getargs()
- {
- Args &a = *(new Args());
- a.addlbl("X coordinate: ");
- a.addlbl("Y coordinate: ");
- a.addlbl("Length of sides: ");
- a.addlbl("Fill character: ");
-
- return a;
- }
-
- // Make instance of triangle
- Triangle *Triangle::makeinstance(Args &a)
- {
- const int MINX = 0;
- const int MINY = 0;
- const int MAXX = 80;
- const int MAXY = 15;
- float tmpx;
- float tmpy;
- float tmps;
- char tmpfill;
-
- // Pop x value
- a.nextval();
- tmpx = atof(a.getval());
- if(tmpx < MINX || tmpx > MAXX){
- cout << "X out of bounds" << endl;
- return 0;
- }
-
- // Pop y value
- a.nextval();
- tmpy = atof(a.getval());
- if(tmpy < MINY || tmpy > MAXY){
- cout << "y out of bounds" << endl;
- return 0;
- }
-
- // Pop s value
- a.nextval();
- tmps = atof(a.getval());
- if(tmps <= 0){
- cout << "triangle cannot have side 0" << endl;
- return 0;
- }
-
- Triangle *t = new Triangle(tmpx, tmpy, tmps);
- assert(t != 0);
-
- //Pop fill value
- a.nextval();
- tmpfill = a.getval()[0];
- t->setfill(tmpfill);
-
- return t;
- }
-
- // Constructor
- Triangle::Triangle(float a, float b, float c)
- : Point(a,b)
- { side = c > 0 ? c : 0; }
-
- // Get side
- float Triangle::gets() const { return side; }
-
- // Draw
- void Triangle::draw() const
- {
- cout << "Trinagle: " << endl;
- cout << "Anchor: ";
- Point::draw();
- cout << endl << "Side: " << gets() << endl;
- }
-
- #include <iostream.h>
- #include "tri.h"
- #include "args.h"
- #include "shape.h"
-
- main()
- {
- Args &a = Triangle::getargs();
- Shape *s;
- char val[5];
-
- for(int i = a.getlblcnt(); i > 0; i--){
- cout << a.getlbl();
- cin >> val;
- a.addval(val);
- a.nextlbl();
- }
-
- s = Triangle::makeinstance(a);
- s->draw();
-
- return 0;
- }
-